﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace Adams.Admin.DataAccess.Console
{
    public static class CreateUser
    {
        public static Adams.Admin.DataTransfer.Console.CreateUser.PageLoad PageLoad(int id)
        {
            var data = new Adams.Admin.DataTransfer.Console.CreateUser.PageLoad();
            data.User = new Adams.Shared.Entities.Console.User();
            data.User.Applications = new Adams.Shared.Entities.Console.ApplicationCollection();

            using (SqlDataReader dr = DB.RunDataReader(@"
IF(@UserID > 0)
BEGIN
    SELECT * FROM Console.Users WHERE UserID = @UserID

    SELECT UA.ApplicationID
    FROM Console.Users U 
    INNER JOIN Console.UsersApplications UA ON U.UserID = UA.UserID
    WHERE U.UserID = @UserID
END

SELECT A.*
FROM Console.Applications A
ORDER BY A.DisplayOrder
", CommandType.Text,
            DB.CreateParameter("@UserID", id, SqlDbType.Int)))
            {
                if (id > 0)
                {
                    dr.Read();
                    data.User.ID = dr["UserID"].DbInt();
                    data.User.FirstName = dr["FirstName"].DbString();
                    data.User.LastName = dr["LastName"].DbString();
                    data.User.Email = dr["Email"].DbString();
                    data.User.IsActive = dr["IsActive"].DbBool();
                    data.User.IsLocked = dr["IsLocked"].DbBool();
                    dr.NextResult();

                    while (dr.Read())
                    {
                        var app = new Adams.Shared.Entities.Console.Application();
                        app.ID = dr["ApplicationID"].DbInt();
                        data.User.Applications.Add(app);
                    }
                    dr.NextResult();
                }

                data.Applications = new Adams.Shared.Entities.Console.ApplicationCollection();
                while (dr.Read())
                {
                    var app = new Adams.Shared.Entities.Console.Application();
                    app.ID = dr["ApplicationID"].DbInt();
                    app.Name = dr["Name"].DbString();
                    data.Applications.Add(app);
                }
            }

            return data;
        }

        public static void SaveUser(DataTransfer.Console.CreateUser.SaveUser input)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var app in input.User.Applications)
            {
                sb.Append("INSERT INTO Console.UsersApplications (UserID, ApplicationID) VALUES (@UserID, " + app.ID + ");");
            }

            DB.Run(@"
IF(@UserID > 0)
BEGIN
    UPDATE Console.Users SET
        FirstName = @FirstName, 
        LastName = @LastName,
        Email = @Email,
        IsActive = @IsActive,
        IsLocked = @IsLocked,
        PasswordHash = @PasswordHash,
        PasswordSalt = @PasswordSalt
    WHERE UserID = @UserID
END
ELSE
BEGIN
    INSERT INTO Console.Users (
        FirstName
        ,LastName
        ,Email
        ,IsActive
        ,IsLocked
        ,PasswordHash
        ,PasswordSalt)
    VALUES (
        @FirstName
        ,@LastName
        ,@Email
        ,@IsActive
        ,@IsLocked
        ,@PasswordHash
        ,@PasswordSalt)

    SET @UserID = SCOPE_IDENTITY()
END

DELETE Console.UsersApplications WHERE UserID = @UserID
" + sb.ToString() + @"
    ", CommandType.Text,
                    DB.CreateParameter("@UserID", input.User.ID),
                    DB.CreateParameter("@FirstName", input.User.FirstName),
                    DB.CreateParameter("@LastName", input.User.LastName),
                    DB.CreateParameter("@Email", input.User.Email),
                    DB.CreateParameter("@IsActive", input.User.IsActive),
                    DB.CreateParameter("@IsLocked", input.User.IsLocked),
                    DB.CreateParameter("@PasswordHash", input.PasswordHash),
                    DB.CreateParameter("@PasswordSalt", input.PasswordSalt));
        }

        public static Adams.Admin.DataTransfer.Console.CreateUser.UserInfo GetUserInfo(int userId, string email)
        {
            var data = new Adams.Admin.DataTransfer.Console.CreateUser.UserInfo();

            using (SqlDataReader dr = DB.RunDataReader(@"
IF (@UserID > 0)
BEGIN
    SELECT *
    FROM Console.Users
    WHERE UserID = @UserID
END

SELECT COUNT(*) AS Count
FROM Console.Users
WHERE Email = @Email
", CommandType.Text,
                DB.CreateParameter("@UserID", userId),
                DB.CreateParameter("@Email", email)))
            {
                if (userId > 0)
                {
                    dr.Read();
                    data.UserID = dr["UserID"].DbInt();
                    data.Email = dr["Email"].DbString();
                    data.PasswordHash = dr["PasswordHash"].DbString();
                    data.PasswordSalt = dr["PasswordSalt"].DbString();
                    dr.NextResult();
                }
                dr.Read();
                data.Exists = dr["Count"].DbInt() > 0;
            }

            return data;
        }
    }
}
